package net.elive.tools;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Pmd52f {
	String MdfDir;

	Pmd52f(String s) {
		MdfDir = s;
	}

	private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

	public String[] readPmd5(String fid) throws IOException {
		File file = new File(MdfDir + fid + ".pmd");
		int p = (int) ((file.length() - 1) / 32);
		String[] str = new String[p];
		byte[] sByte = new byte[32];
		FileInputStream fis = new FileInputStream(file);
		BufferedInputStream bis = new BufferedInputStream(fis);
		int ver = bis.read();
		if (ver == 1) {
			for (int i = 0; i < p; i++) {
				bis.read(sByte);
				str[i] = new String(sByte);
			}
		}
		bis.close();
		return str;
	}

	public byte[] savePmd5(byte[][] mdb, String MdfDir, long length,
			String fileExten) {
		byte[] extendName = fileExten.getBytes();
		byte[] mdb1 = new byte[1 + extendName.length + 8 + 16 * mdb.length];
		mdb1[0] = (byte) extendName.length;
		System.arraycopy(extendName, 0, mdb1, 1, extendName.length);
		ByteLongSwitch.toByteArray(length, mdb1, 1 + extendName.length);
		File dir = new File(MdfDir);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		for (int i = 0; i < mdb.length; i++) {
			System.arraycopy(mdb[i], 0, mdb1, 1 + extendName.length + 8 + i
					* 16, 16);
		}
		MessageDigest mDigest;
		byte[] mdb2 = null;
		String Name = null;
		try {
			mDigest = MessageDigest.getInstance("MD5");
			mDigest.update(mdb1);
			mdb2 = mDigest.digest();
			Name = byteArrayToHexString(mdb2);
			String fName = Name + ".pmd";
			File file = new File(MdfDir + "\\" + fName);
			System.out.println("TTTTTTTTTTTT  " + file.getAbsolutePath());
			file.createNewFile();
			FileOutputStream fos = new FileOutputStream(file);
			BufferedOutputStream bos = new BufferedOutputStream(fos);
			DataOutputStream dos = new DataOutputStream(bos);
			// д��汾��
			dos.writeShort(0);
			// д��mdf��Ϣ
			dos.write(mdb1);
			dos.flush();
			dos.close();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// System.out.println("RRRRRR " +Pmd52f.byteArrayToHexString(mdb)+new
		// String(mdb));
		return mdb2;
	}

	/**
	 * 字节数组转换16进制字符串
	 * @param b
	 * @return
	 */
	public static String byteArrayToHexString(byte[] b) {
		StringBuffer resultSb = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			resultSb.append(byteToHexString(b[i]));
		}
		return resultSb.toString();
	}
	
	public static byte[] hexStringToByteArray(String strA) {
		ByteArrayOutputStream result = new ByteArrayOutputStream();
		// alle Hex-Zeichen konvertieren, den Rest Ignorieren
		// jedes Zeichen stellt einen 4-Bit Wert dar
		byte sum = (byte) 0x00;
		boolean nextCharIsUpper = true;

		for (int i = 0; i < strA.length(); i++) {
			char c = strA.charAt(i);

			switch (Character.toUpperCase(c)) {

			case '0':
				if (nextCharIsUpper) {
					sum = (byte) 0x00;
					nextCharIsUpper = false;
				} else {
					sum |= (byte) 0x00;
					result.write(sum);
					nextCharIsUpper = true;
				}
				break;

			case '1':
				if (nextCharIsUpper) {
					sum = (byte) 0x10;
					nextCharIsUpper = false;
				} else {
					sum |= (byte) 0x01;
					result.write(sum);
					nextCharIsUpper = true;
				}
				break;

			case '2':
				if (nextCharIsUpper) {
					sum = (byte) 0x20;
					nextCharIsUpper = false;
				} else {
					sum |= (byte) 0x02;
					result.write(sum);
					nextCharIsUpper = true;
				}
				break;

			case '3':
				if (nextCharIsUpper) {
					sum = (byte) 0x30;
					nextCharIsUpper = false;
				} else {
					sum |= (byte) 0x03;
					result.write(sum);
					nextCharIsUpper = true;
				}
				break;

			case '4':
				if (nextCharIsUpper) {
					sum = (byte) 0x40;
					nextCharIsUpper = false;
				} else {
					sum |= (byte) 0x04;
					result.write(sum);
					nextCharIsUpper = true;
				}
				break;

			case '5':
				if (nextCharIsUpper) {
					sum = (byte) 0x50;
					nextCharIsUpper = false;
				} else {
					sum |= (byte) 0x05;
					result.write(sum);
					nextCharIsUpper = true;
				}
				break;

			case '6':
				if (nextCharIsUpper) {
					sum = (byte) 0x60;
					nextCharIsUpper = false;
				} else {
					sum |= (byte) 0x06;
					result.write(sum);
					nextCharIsUpper = true;
				}
				break;

			case '7':
				if (nextCharIsUpper) {
					sum = (byte) 0x70;
					nextCharIsUpper = false;
				} else {
					sum |= (byte) 0x07;
					result.write(sum);
					nextCharIsUpper = true;
				}
				break;

			case '8':
				if (nextCharIsUpper) {
					sum = (byte) 0x80;
					nextCharIsUpper = false;
				} else {
					sum |= (byte) 0x08;
					result.write(sum);
					nextCharIsUpper = true;
				}
				break;

			case '9':
				if (nextCharIsUpper) {
					sum = (byte) 0x90;
					nextCharIsUpper = false;
				} else {
					sum |= (byte) 0x09;
					result.write(sum);
					nextCharIsUpper = true;
				}
				break;

			case 'A':
				if (nextCharIsUpper) {
					sum = (byte) 0xA0;
					nextCharIsUpper = false;
				} else {
					sum |= (byte) 0x0A;
					result.write(sum);
					nextCharIsUpper = true;
				}
				break;

			case 'B':
				if (nextCharIsUpper) {
					sum = (byte) 0xB0;
					nextCharIsUpper = false;
				} else {
					sum |= (byte) 0x0B;
					result.write(sum);
					nextCharIsUpper = true;
				}
				break;

			case 'C':
				if (nextCharIsUpper) {
					sum = (byte) 0xC0;
					nextCharIsUpper = false;
				} else {
					sum |= (byte) 0x0C;
					result.write(sum);
					nextCharIsUpper = true;
				}
				break;

			case 'D':
				if (nextCharIsUpper) {
					sum = (byte) 0xD0;
					nextCharIsUpper = false;
				} else {
					sum |= (byte) 0x0D;
					result.write(sum);
					nextCharIsUpper = true;
				}
				break;

			case 'E':
				if (nextCharIsUpper) {
					sum = (byte) 0xE0;
					nextCharIsUpper = false;
				} else {
					sum |= (byte) 0x0E;
					result.write(sum);
					nextCharIsUpper = true;
				}
				break;

			case 'F':
				if (nextCharIsUpper) {
					sum = (byte) 0xF0;
					nextCharIsUpper = false;
				} else {
					sum |= (byte) 0x0F;
					result.write(sum);
					nextCharIsUpper = true;
				}
				break;
			}
		}

		if (!nextCharIsUpper) {
			throw new RuntimeException(
					"The String did not contain an equal number of hex digits");
		}

		return result.toByteArray();
	}
	/**
	 * 字节转换16进制字符串
	 * @param b
	 * @return
	 */
	static String byteToHexString(byte b) {
		int n = b;
		if (n < 0) {
			n = 256 + n;
		}
		int d1 = n / 16;
		int d2 = n % 16;
		return hexDigits[d1] + hexDigits[d2];
	}

}
